<route lang="jsonc" type="page">{
    "layout": "page",
    "style": {
        "navigationBarTitleText": "收货人信息"
    }
}</route>

<script setup lang='ts'>
import { getOrderAddress } from '@/api/address'
import { onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'

const title = ref('收货人信息')
const left = ref(true)
const list = ref<any[]>([])  // 使用any[]类型更安全

// 安全获取属性函数
const safeGet = (obj: any, path: string, defaultValue = '') => {
    return path.split('.').reduce((acc, key) => {
        try {
            return acc && acc[key] !== undefined ? acc[key] : defaultValue
        } catch {
            return defaultValue
        }
    }, obj)
}

// 跳转到编辑页面
const goToEdit = (addressId: string) => {
    if (!addressId) {
        console.error('地址ID不能为空')
        return
    }
    uni.navigateTo({
        url: '/pages/setaddress/setaddress?type=edit&id=' + addressId
    })
}

// 跳转到添加页面
const goToAdd = () => {
    uni.navigateTo({
        url: '/pages/setaddress/setaddress'
    })
}

// 加载地址数据
const loadAddress = async () => {
    try {
        const res = await getOrderAddress()
        console.log('API Response:', res)
        
        // 安全处理响应数据
        if (safeGet(res, 'code') === 10000) {
            const data = safeGet(res, 'data', [])
            list.value = Array.isArray(data) ? data : [data]
            
            // 调试日志
            console.log('Processed Address List:', list.value)
        } else {
            console.error('API Error:', safeGet(res, 'message', '未知错误'))
        }
    } catch (error) {
        console.error('请求失败:', error)
        uni.showToast({
            title: '加载地址信息失败',
            icon: 'none'
        })
    }
}

// 使用 onShow 替代 onLoad 确保每次进入页面都刷新数据
onShow(() => {
    loadAddress()
})
</script>

<template>
    <view>
        <view class="m-30rpx">
            <view v-if="list.length === 0" style="display: flex;flex-direction: column;align-items: center;">
                <image src="/static/mine/unfound@1x.png" class="w-100rpx h-100rpx" mode="scaleToFill" style="margin-top: 60rpx;"/>
                <view style="font-size: 28rpx; color:#979797;">暂无收货人信息</view>
            </view>

            <view v-else>
                <view 
                    v-for="(item, index) in list" 
                    :key="index" 
                    style="background-color: #FAFAFA;"
                    class="p-30rpx rounded-lg mb-30rpx" 
                    :class="{
                        'border-2 border-[#16C2A3] bg-[#2CB5A511]': safeGet(item, 'isDefault') === 1,
                        'bg-white': safeGet(item, 'isDefault') !== 1
                    }"
                >
                    <view style="display: flex;justify-content: space-between;align-items: center;">
                        <view>
                            <view class="flex items-center justify-between mb-10rpx">
                                <text 
                                    v-if="safeGet(item, 'isDefault') === 1" 
                                    class="text-white text-24rpx bg-[#16C2A3] rounded-full px-12rpx py-4rpx "
                                >
                                    默认
                                </text>
                                <text class="text-32rpx font-bold text-gray-800 w-200rpx mb-10rpx ml--50rpx">
                                    {{ safeGet(item, 'receiver', '') }}
                                </text>
                                <text class="text-26rpx text-[#6f6f6f] ml-20rpx">
                                    {{ safeGet(item, 'mobile', '') }}
                                </text>
                            </view>
                            <view class="text-26rpx text-[#6f6f6f]">
                                {{ safeGet(item, 'province', '') }}
                                {{ safeGet(item, 'city', '') }}
                                {{ safeGet(item, 'county', '') }}
                                {{ safeGet(item, 'addressDetail', '') }}
                            </view>
                        </view>
                        <image 
                            src="/static/image/editimg.png" 
                            class="w-30rpx h-30rpx" 
                            mode="scaleToFill"
                            @click="goToEdit(safeGet(item, 'id', ''))" 
                        />
                    </view>
                </view>
            </view>

            <view
                style="background-color:#2CB5A5;border-radius:44rpx;font-size:32rpx;height:88rpx;color:#ffffff;display:flex;align-items:center;justify-content:center;position: fixed;bottom: 10rpx;width: 690rpx;"
                @click="goToAdd"
            >
                新增收货地址
            </view>
        </view>
    </view>
</template>

<style lang='scss' scoped>
/* 样式 */
</style>